/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Form;

import BLL.BLLOrder;
import BLL.BLLOrderDetail;
import BLL.BLLProduct;
import DTO.DTOOrder;
import DTO.DTOOrderDetail;
import DTO.DTOProduct;
import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Pham
 */
public class JDialogAddOrder extends javax.swing.JDialog {
    BLLProduct bllPro;
    BLLOrder bllOrd;
    BLLOrderDetail bllOrdDetail;
    DefaultTableModel modelPro;
    DefaultTableModel modelOrder;
    /**
     * Creates new form JDialogAddOrder
     * @param parent
     * @param modal
     */
    public JDialogAddOrder(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        bllPro = new BLLProduct();
        bllOrd = new BLLOrder();
        bllOrdDetail = new BLLOrderDetail();
        initComponents();
        loadDatatoTable();
        modelOrder = new DefaultTableModel();
        String[] colsOrder = {"Mã SP", "Tên SP", "Hãng SX", "Số lượng đặt mua"};
        modelOrder.setColumnIdentifiers(colsOrder);
        tbOrder.setModel(modelOrder);
        
        enableButton();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        ScrollpaneProduct = new javax.swing.JScrollPane();
        tbProduct = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        txtSearchName = new javax.swing.JTextField();
        btnSearch = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        btnAddAll = new javax.swing.JButton();
        btnAddItem = new javax.swing.JButton();
        btnRemoveItem = new javax.swing.JButton();
        btnRemoveAll = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbOrder = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        txtGuessName = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtGuessAddress = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtGuessPhone = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtGuessEmail = new javax.swing.JTextField();
        btnSave = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        btnNew = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Danh sách SP", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 102)));
        jPanel2.setPreferredSize(new java.awt.Dimension(390, 23));

        ScrollpaneProduct.setAutoscrolls(true);
        ScrollpaneProduct.setPreferredSize(new java.awt.Dimension(358, 245));

        tbProduct.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbProduct.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbProductMousePressed(evt);
            }
        });
        tbProduct.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tbProductFocusLost(evt);
            }
        });
        ScrollpaneProduct.setViewportView(tbProduct);

        jLabel2.setText("Tìm sản phẩm");

        btnSearch.setText("Tìm");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ScrollpaneProduct, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtSearchName, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtSearchName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearch))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ScrollpaneProduct, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                .addContainerGap())
        );

        btnAddAll.setText(">>");
        btnAddAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddAllActionPerformed(evt);
            }
        });

        btnAddItem.setText(">");
        btnAddItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddItemActionPerformed(evt);
            }
        });

        btnRemoveItem.setText("<");
        btnRemoveItem.setEnabled(false);
        btnRemoveItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveItemActionPerformed(evt);
            }
        });

        btnRemoveAll.setText("<<");
        btnRemoveAll.setEnabled(false);
        btnRemoveAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveAllActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btnAddItem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnAddAll, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnRemoveItem, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnRemoveAll, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(btnAddItem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAddAll)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnRemoveItem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnRemoveAll)
                .addContainerGap(31, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Danh sách sản phẩm cần tạo đơn hàng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 102, 0)));

        tbOrder.setAutoCreateRowSorter(true);
        tbOrder.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(tbOrder);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 102, 102));
        jLabel1.setText("Tạo đơn hàng");

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin khách hàng", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 153, 51)));

        jLabel3.setText("Tên khách hàng");

        jLabel4.setText("Địa chỉ");

        jLabel5.setText("Số điện thoại");

        txtGuessPhone.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtGuessPhoneKeyTyped(evt);
            }
        });

        jLabel6.setText("Email");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtGuessPhone)
                            .addComponent(txtGuessEmail)
                            .addComponent(txtGuessName))
                        .addGap(102, 102, 102))
                    .addComponent(txtGuessAddress))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtGuessName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtGuessAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtGuessPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtGuessEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnSave.setText("Lưu");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnClose.setText("Đóng");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        btnNew.setText("Làm lại");
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(312, 312, 312)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(9, 9, 9)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnNew, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(67, 67, 67)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE))))
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnSave, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnClose, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(btnNew, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbProductMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbProductMousePressed
        //btnAddItem.setEnabled(true);
    }//GEN-LAST:event_tbProductMousePressed

    private void tbProductFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tbProductFocusLost
        //btnAddItem.setEnabled(false);
    }//GEN-LAST:event_tbProductFocusLost

    private void btnAddAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddAllActionPerformed
        try{
            DefaultTableModel model = (DefaultTableModel)tbProduct.getModel();
            DefaultTableModel modelIm = (DefaultTableModel)tbOrder.getModel();
            if(model != null)
            {
                int rows = tbProduct.getRowCount();
                if(rows > 0){
                    //modelImp = new DefaultTableModel();
                    for(int i = rows - 1; i >= 0; i--){
                        int id = (int)tbProduct.getValueAt(i, 0);
                        if(!checkExist(id, tbOrder)){
                            DTOProduct dtoProduct = bllPro.getProductbyID(id);
                            modelIm.addRow(new Object[]{dtoProduct.getProductID(), dtoProduct.getProductName(),dtoProduct.getManu().getManuName(), dtoProduct.getProductQuantity()});

                        }
                        model.removeRow(i);
                    }
                    //                    tbImport.setModel(modelImp);

                    //                    for(int i = rows - 1; i >= 0; i--){
                        //                        model.removeRow(i);
                        //                    }
                }

                enableButton();
            }

        }
        catch(Exception ex)
        {
            JOptionPane.showConfirmDialog(null, "Error");
        }
    }//GEN-LAST:event_btnAddAllActionPerformed

    private void btnAddItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddItemActionPerformed
        try{
            DefaultTableModel model = (DefaultTableModel)tbProduct.getModel();
            modelOrder = (DefaultTableModel)tbOrder.getModel();
            int[] rows = tbProduct.getSelectedRows();
            for(int i = rows.length - 1; i >= 0; i--)
            {
                int id = Integer.valueOf(tbProduct.getValueAt(rows[i], 0).toString());
                if(!checkExist(id, tbOrder)){
                    DTOProduct Pro = bllPro.getProductbyID(id);
                    modelOrder.addRow(new Object[]{Pro.getProductID(), Pro.getProductName(), Pro.getManu().getManuName(), 1});

                }

            }
            for(int i = rows.length - 1; i >= 0; i--)
            {
                model.removeRow(rows[i]);
            }
            //tbImport.setModel(modelImp);
            enableButton();
        }
        catch(Exception ex)
        {
            System.err.printf(ex.getMessage());
        }
    }//GEN-LAST:event_btnAddItemActionPerformed

    private void btnRemoveItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveItemActionPerformed
        try
        {
            modelOrder = (DefaultTableModel)tbOrder.getModel();
            DefaultTableModel modelPr = (DefaultTableModel)tbProduct.getModel();
            int[] rows = tbOrder.getSelectedRows();
            for(int i = rows.length - 1; i >= 0; i--)
            {
                int id = Integer.valueOf(tbOrder.getValueAt(rows[i], 0).toString());
                if(!checkExist(id, tbProduct)){
                    DTOProduct Pro = bllPro.getProductbyID(id);
                    modelPr.addRow(new Object[]{Pro.getProductID(), Pro.getProductName(), Pro.getManu().getManuName(), Pro.getProductQuantity()});

                }
            }
            for(int i = rows.length - 1; i >= 0; i--)
            {
                modelOrder.removeRow(rows[i]);
            }
            //tbProduct.setModel(modelPro);
            enableButton();
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }//GEN-LAST:event_btnRemoveItemActionPerformed

    private void btnRemoveAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveAllActionPerformed
        modelOrder = (DefaultTableModel)tbOrder.getModel();
        DefaultTableModel modelPr = (DefaultTableModel)tbProduct.getModel();
        if(modelOrder != null)
        {
            int rows = tbOrder.getRowCount();

            for(int i = rows - 1; i >= 0; i--){
                int id = (int)tbOrder.getValueAt(i, 0);
                if(!checkExist(id, tbProduct)){
                    DTOProduct dtoPro = bllPro.getProductbyID(id);
                    modelPr.addRow(new Object[]{dtoPro.getProductID(), dtoPro.getProductName(), dtoPro.getManu().getManuName(), dtoPro.getProductQuantity()});
                }
                modelOrder.removeRow(i);
            }
            //            tbProduct.setModel(modelPro);
            //loadDatatoTable();
            enableButton();
        }
    }//GEN-LAST:event_btnRemoveAllActionPerformed

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCloseActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        try{
            if("".equals(checkvalue()))
            {
                String guessName = txtGuessName.getText().trim();
                String guessAddress = txtGuessAddress.getText().trim();
                String guessEmail = txtGuessEmail.getText().trim();
                String guessPhone = txtGuessPhone.getText().trim();
                Date date = new Date();
                SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssS");
                String orderId = format.format(date);
                DTOOrder dtoOrder = new DTOOrder();
                dtoOrder.setOrderId(orderId);
                dtoOrder.setOrderDateCreated(date);
                dtoOrder.setOrderGuessName(guessName);
                dtoOrder.setOrderGuessAddress(guessAddress);
                dtoOrder.setOrderGuessEmail(guessEmail);
                dtoOrder.setOrderGuessPhone(guessPhone);
                dtoOrder.setOrderState(0);
                if(bllOrd.createOrder(dtoOrder))
                {
                    DTOOrderDetail dtoOrdDetail = new DTOOrderDetail();
                    dtoOrdDetail.setDtoOrder(dtoOrder);
                    int count = 0;
                    
                    for(int i = tbOrder.getRowCount() -1; i >= 0; i--)
                    {
                        DTOProduct dtoPro = new DTOProduct();
                        dtoPro.setProductID((int)tbOrder.getValueAt(i, 0));
                        dtoOrdDetail.setDtoPro(dtoPro);
                        dtoOrdDetail.setQuantity((int)tbOrder.getValueAt(i, 3));
                        if(bllOrdDetail.createOrderDetail(dtoOrdDetail))
                            count++;
                    }
                    if(count == tbOrder.getRowCount())
                    {
                        JOptionPane.showMessageDialog(null, "Đã tạo đơn hàng thành công");
                        
                    }
                    else
                        JOptionPane.showMessageDialog(null, "Tạo đơn hàng bị lỗi");
                }
                else
                    JOptionPane.showMessageDialog(null, "Không thể tạo đơn hàng");
                
            }
            else
            {
                JOptionPane.showMessageDialog(null, checkvalue());
                //JOptionPane.showMessageDialog(null, tbOrder.getRowCount());
            }
        }
        catch(Exception ex){
            JOptionPane.showMessageDialog(null, "Lỗi: không thể lưu đơn hàng");
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
        txtGuessName.setText("");
        txtGuessAddress.setText("");
        txtGuessEmail.setText("");
        txtGuessPhone.setText("");
        modelOrder = (DefaultTableModel)tbOrder.getModel();
        if(modelOrder != null)
        {
            int rows = tbOrder.getRowCount();

            for(int i = rows - 1; i >= 0; i--){
                
                modelOrder.removeRow(i);
            }
            loadDatatoTable();
            enableButton();
        }
    }//GEN-LAST:event_btnNewActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        try{
            modelPro = (DefaultTableModel)tbProduct.getModel();
            if(modelPro != null)
            {
                for(int i = modelPro.getRowCount() - 1; i >= 0; i--)
                {
                    modelPro.removeRow(i);
                }
                for(DTOProduct dtoPro : bllPro.searchProToCreateOrder(txtSearchName.getText().trim()))
                {
                    modelPro.addRow(new Object[]{dtoPro.getProductID(), dtoPro.getProductName(), dtoPro.getManu().getManuName(), dtoPro.getProductQuantity()});
                }
            }
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "không thể tìm");
        }
    }//GEN-LAST:event_btnSearchActionPerformed

    private void txtGuessPhoneKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtGuessPhoneKeyTyped
        char c = evt.getKeyChar();
        if(!Character.isDigit(c) || c == KeyEvent.VK_BACK_SPACE || c == KeyEvent.VK_DELETE)
        {
            evt.consume();
        }
    }//GEN-LAST:event_txtGuessPhoneKeyTyped

    //method check value input
    private String checkvalue()
    {
        String result = "";
        if(tbOrder.getRowCount() == 0)
            result += "Bạn phải chọn sản phẩm cần đặt hàng";
        if("".equals(txtGuessName.getText().trim()))
            result += "Bạn phải nhập tên khách hàng\n";
        if("".equals(txtGuessAddress.getText().trim()))
            result += "Bạn phải nhập địa chỉ khách hàng\n";
        if("".equals(txtGuessEmail.getText().trim()))
            result += "Bạn phải nhập mail khách hàng\n";
        if("".equals(txtGuessPhone.getText().trim()))
            result += "Bạn phải nhập số điện thoại khách hàng";
        return result;
    }
    
    //method load data to table
    private void loadDatatoTable()
    {
        modelPro = bllPro.getProducttoImport();
        tbProduct.setModel(modelPro);
    }
    
    //method enable button
    private void enableButton()
    {
        
        int countPro = tbProduct.getRowCount();
        int countIm = tbOrder.getRowCount();
        if(countPro <= 0)
        {
            btnAddAll.setEnabled(false);
            btnAddItem.setEnabled(false);
        }
        else
        {
            btnAddAll.setEnabled(true);
            btnAddItem.setEnabled(true);
        }
        if(countIm <= 0)
        {
            btnRemoveAll.setEnabled(false);
            btnRemoveItem.setEnabled(false);
        }
        else
        {
            btnRemoveAll.setEnabled(true);
            btnRemoveItem.setEnabled(true);
        }
        
    }
    
    
    //method check exist of item in table
    private boolean checkExist(int id, JTable table)
    {
        int countRow = table.getRowCount();
        if(countRow > 0)
        {
            for(int i = countRow - 1; i >= 0; i--){
                if((int)table.getValueAt(i, 0) == id)
                    return true;
            }
        }
        return false;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane ScrollpaneProduct;
    private javax.swing.JButton btnAddAll;
    private javax.swing.JButton btnAddItem;
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnRemoveAll;
    private javax.swing.JButton btnRemoveItem;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSearch;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbOrder;
    private javax.swing.JTable tbProduct;
    private javax.swing.JTextField txtGuessAddress;
    private javax.swing.JTextField txtGuessEmail;
    private javax.swing.JTextField txtGuessName;
    private javax.swing.JTextField txtGuessPhone;
    private javax.swing.JTextField txtSearchName;
    // End of variables declaration//GEN-END:variables
}
